<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="utf-8">
    <title>本周菜单</title>
    <link rel="stylesheet" th:href="@{/component/pear/css/pear.css}" />
</head>
<body class="pear-container">
<div class="layui-card">
    <div class="layui-card-body">
        <form class="layui-form" action="">
            <div class="layui-form-item" style="padding-left: 20px">
                <div class="layui-form-item layui-inline">
                    <label class="layui-form-label">菜品名</label>
                    <div class="layui-input-inline">
                        <input type="text" name="name" placeholder="请输入菜品名" class="layui-input">
                    </div>
                </div>
                <div class="layui-form-item layui-inline">
                    <button class="pear-btn pear-btn-md pear-btn-primary" lay-submit lay-filter="user-query">
                        <i class="layui-icon layui-icon-search"></i>
                        查询
                    </button>
                    <button type="reset" class="pear-btn pear-btn-md">
                        <i class="layui-icon layui-icon-refresh"></i>
                        重置
                    </button>
                </div>
            </div>
        </form>
    </div>
</div>
<div class="layui-card">
    <div class="layui-card-body">
        <table id="user-table" lay-filter="user-table"></table>
    </div>
</div>

<script type="text/html" id="user-toolbar">
    <button class="pear-btn pear-btn-primary pear-btn-md" lay-event="add">
        <i class="layui-icon layui-icon-add-circle"></i>
        批量复用
    </button>
</script>

<script type="text/html" id="img">
    {{#if (d.picture == null) { }}
    <img th:src="@{/picture/None.png}" style="width: 50px; height: 50px" />
    {{# }else if(d.picture != null){ }}
    <img src="/picture/{{d.picture}}" style="width: 50px; height: 50px" />
    {{# } }}
</script>

<script type="text/html" id="user-bar">
    <button class="pear-btn pear-btn-warming pear-btn-sm" lay-event="remove">修改</button>
</script>

<script th:src="@{/component/layui/layui.js}"></script>
<script th:src="@{/component/pear/pear.js}"></script>
<script>
    layui.use(['table', 'form', 'jquery','common'], function() {
        let table = layui.table;
        let form = layui.form;
        let $ = layui.jquery;
        let common = layui.common;

        let cols = [
            [
                {
                    type: 'checkbox'
                },
                {
                    title: '菜品名称',
                    field: 'name',
                    align: 'center',
                    width: 120
                },
                {
                    title: '分类',
                    field: 'category',
                    align: 'center',
                    width: 100
                },
                {
                    title: '计量单位',
                    field: 'unit',
                    align: 'center',
                    width: 100
                },
                {
                    title: '价格',
                    field: 'price',
                    align: 'center',
                    width: 100
                },
                {
                    title: '创建时间',
                    field: 'createTime',
                    align: 'center',
                    width: 180
                },
                {
                    title: '图片',
                    field: 'picture',
                    align: 'center',
                    width: 100,
                    templet: "#img",
                    style: 'height:50px;'
                },
                {
                    title: '操作',
                    toolbar: '#user-bar',
                    align: 'center',
                    width: 100
                }
            ]
        ]

        table.render({
            elem: '#user-table',
            url: '/menu/pageThisWeek',
            page: true,
            parseData:function(res) {
                return {
                    "code" : 0,
                    "msg" : "",
                    "count" : res.data.total,
                    "data": res.data.records
                }
            },
            cols: cols,
            skin: 'line',
            limits: [10, 20],
            toolbar: '#user-toolbar',
            defaultToolbar: ['filter', 'print', 'exports']
        });

        table.on('toolbar(user-table)', function(obj) {
            if (obj.event === 'add') {

                var checkIds = common.checkField(obj,'menuId');

                if (checkIds === "") {
                    layer.msg("未选中数据", {
                        icon: 3,
                        time: 1000
                    });
                    return false;
                }

                layer.confirm('确定要复用这些菜品到下一周吗', {
                    icon: 3,
                    title: '提示'
                }, function(index) {
                    layer.close(index);
                    let loading = layer.load();
                    $.ajax({
                        url: "/menu/multiplex/" + checkIds,
                        dataType: 'json',
                        type: 'post',
                        success: function(result) {
                            layer.close(loading);
                            if (result.code === 200) {
                                layer.msg(result.data, {
                                    icon: 1,
                                    time: 1000
                                }, function() {
                                    table.reload('user-table');
                                });
                            } else {
                                layer.msg(result.msg, {
                                    icon: 2,
                                    time: 3000
                                });
                            }
                        }
                    })
                });
            }
        });

        table.on('tool(user-table)', function(obj) {
            if (obj.event === 'remove') {
                window.remove(obj);
            }
        });

        form.on('submit(user-query)', function(data) {
            table.reload('user-table', {
                where: data.field
            })
            return false;
        });


        window.remove = function(obj) {
            layer.open({
                type: 2,
                title: '修改',
                shade: 0.1,
                area: ['500px', '400px'],
                content: '/back/toMenuEdit/' + obj.data['menuId']
            });
        }

        window.refresh = function() {
            table.reload('user-table');
        }
    })
</script>
</body>
</html>
